const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    ValiStatus: false,
    name:'',
    mobile:'',
    region: [],
    detail:'',
    type:'add',
    id:'',
    addressDetaildata:{
      name:'',
      mobile:'',
      address:'',
      isDefault:0
    },
  },
  
  onLoad: function (options) {
    var _Detail = {};
    if(options.type=='add'){
      wx.setNavigationBarTitle({
        title: '新增地址'
      })
      this.setData({
        type:'add'
      })
    }else{
      wx.setNavigationBarTitle({
        title: '修改地址'
      })
      this.setData({
        type:'edit'
      })
    }
    if(options.id){
      this.setData({
        id:options.id
      })
      this.addressDetail()
    }
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  addressDetail(){
    var data={
      addressId:this.data.id
    }
    Controller.addressDetail(data).then(res=>{
      if(res.data.baseData){
        this.setData({
          addressDetaildata:res.data.baseData,
          region:[res.data.baseData.province,res.data.baseData.city,res.data.baseData.area]
        })
      }
    })
  },
  default(){
    this.data.addressDetaildata.isDefault=this.data.addressDetaildata.isDefault==0?1:0
    this.setData({
      addressDetaildata:this.data.addressDetaildata
    })
  },
  ValiData(status) {
      let flag = true;
      let msg = '';
      if (!this.data.addressDetaildata.name ) {
          msg = '请输入收货人姓名';
          flag = false;
      }
      if (!this.data.addressDetaildata.mobile) {
          if (msg == '') msg = '请输入联系电话';
          flag = flag && false;
      }

      console.log(this.data.addressDetaildata.mobile)
      if (!/^1[0-9]{10}$/.test(this.data.addressDetaildata.mobile)) {
          if (msg == '') msg = '联系电话格式错误';
          flag = flag && false;
      }
      if (!this.data.addressDetaildata.address) {
          if (msg == '') msg = '详细地址不能为空';
          flag = flag && false;
      }
      if (!this.data.region[2]) {
          if (msg == '') msg = '选择地区不能为空';
          flag = flag && false;
      }
      if (status && msg) {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 800
          });;
      }
      this.setData({
          ValiStatus: flag
      })
      return flag;
  },
  //保存编辑地址
  saveEdit() {
    // this.ValiData(true)
    let flag = true;
    let msg = '';
    if (!this.data.addressDetaildata.name ) {
        msg = '请输入收货人姓名';
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 800
        });
        this.setData({
            ValiStatus: false
        })
        return;
    }
    if (!this.data.addressDetaildata.mobile) {
        if (msg == '') msg = '请输入联系电话';
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 800
        });
        this.setData({
            ValiStatus: false
        })
        return;
    }
    if (!/^1[0-9]{10}$/.test(this.data.addressDetaildata.mobile)) {
        if (msg == '') msg = '联系电话格式错误';
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 800
        });
        this.setData({
            ValiStatus: false
        })
        return;
    }
    if (!this.data.addressDetaildata.address) {
        if (msg == '') msg = '详细地址不能为空';
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 800
        });
        this.setData({
            ValiStatus: false
        })
        return;
    }
    if (!this.data.region[2]) {
        if (msg == '') msg = '选择地区不能为空';
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 800
        });
        this.setData({
            ValiStatus: false
        })
        return;
    }

    this.setData({
        ValiStatus: true
    })

    var data={
      name:this.data.addressDetaildata.name,
      mobile:this.data.addressDetaildata.mobile,
      province:this.data.region[0] || '',
      city:this.data.region[1] || '',
      area:this.data.region[2] || '',
      address:this.data.addressDetaildata.address,
      isDefault:this.data.addressDetaildata.isDefault,
    }
    if(this.data.type=='edit'){
      data.addressId=this.data.id;
      Controller.editAddress(data).then(res=>{
          wx.showToast({
            title: '保存成功'
          })
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            })
          },1500)
  
      })
    }else{
      data.isDefault=this.data.addressDetaildata.isDefault
      Controller.addAddress(data).then(res=>{
          wx.showToast({
            title: '保存成功'
          })
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            })
          },1500)
    
      })
    }
  },
  //表单事件绑定
  BindNameChange(e) {
    this.setData({
      'addressDetaildata.name': e.detail.value
    })
    this.ValiData();
  },
  BindMobileChange(e) {
    this.setData({
      'addressDetaildata.mobile': e.detail.value
    })
    this.ValiData();
  },
  BindAddressChange(e) {
    this.setData({
      'addressDetaildata.address': e.detail.value
    })
    this.ValiData();
  },
  //Picker改变事件
  bindRegionChange(e) {
    this.setData({
        region: e.detail.value,
    })
    this.ValiData();
  }
})